<?php 
/*
**********************************************
JoomLeague RANDOM PLAYER MODULE
Copyright (c) 2007 JoomLeague.de
**********************************************
This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* RANDOM PLAYER MODULE
*
* 2007-07-31 - Version 0.90
*
* Displays a random player of a choosen project and team.
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JoomLeague at:
http://www.joomleague.de//
**********************************************
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

if (file_exists($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php')) {
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php');
} 
else{
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/english.php');
}

require_once 'administrator/components/com_joomleague/joomleague.class.php';

$params = mosParseParams( $module->params );
$show = $params->show; //default 0
$show_team = $params->show_team; //default 1

$teamtoolid = $params->team;

//$teamtool = new TeamTool($database);
//$teamtool->load($teamtoolid);

//$project_id = $teamtool->project_id;
//$team_id = $teamtool->team_id;

//$project = new JoomLeague( $database );
//$project->load($teamtool->project_id);

$where='';

$query = "SELECT pt.id, pl.firstname,pl.lastname, pt.picture, pt.project_id, t.name, p.name as projectname
            FROM #__joomleague_playertool pt
       LEFT JOIN #__joomleague_players pl ON pl.id = pt.player_id 
       LEFT JOIN #__joomleague_teams t ON pt.team_id = t.id
       LEFT JOIN #__joomleague_team_joomleague tt ON tt.team_id = t.id
       LEFT JOIN #__joomleague p ON pt.project_id = p.id";

if ( $params->project != 'all' ){
	$where = sprintf(" WHERE pt.project_id IN (%s)", $params->project);
}

if ( $params->team != 'all' ){
	if ( $where !='' ) $where .= " AND "; else $where = " WHERE ";
	$where .= sprintf("t.id IN (%s)", $teamtoolid);
}
	
$query .= $where." ORDER BY rand() LIMIT 1";

$database->setQuery($query);
$rows = $database->loadObjectList();
?>

<?php if($show == 1 && (isset($rows[0]->projectname))){?><div align="center"><b><?php echo $rows[0]->projectname;?></b></div><?php };?>

<?php if($show_team == 1 && (isset($rows[0]->name))){?><div align="center"><b><?php echo $rows[0]->name;?></b></div><?php };?>

<div align="center">
<?php if($params->link_picture == 1 && (isset($rows[0]->project_id))) echo '<a href="'.sefRelToAbs("index.php?option=com_joomleague&amp;func=showPlayer&amp;p=".$rows[0]->project_id."&amp;pid=".$rows[0]->id."&amp;Itemid=".$Itemid).'">';?>

<?php if (isset($rows[0]->picture)) { ?><img src="<?php echo $mosConfig_live_site;?>/<?php echo $rows[0]->picture;?>" border="0" alt="<?php echo $rows[0]->lastname; if ($rows[0]->firstname) echo ", ".$rows[0]->firstname;?>" title="<?php echo $rows[0]->lastname; if ($rows[0]->firstname) echo ", ".$rows[0]->firstname;?>" /><?php } ?>
<?php if($params->link_picture == 1){;?></a><?php } ?><br />
  
<?php if($params->link_name == 1 && (isset($rows[0]->project_id)))  echo '<a href="'.sefRelToAbs("index.php?option=com_joomleague&amp;func=showPlayer&amp;p=".$rows[0]->project_id."&amp;pid=".$rows[0]->id."&amp;Itemid=".$Itemid).'">';
if($params->display == 0) { if (isset($rows[0]->lastname)) echo '<b>'.$rows[0]->lastname; if (isset($rows[0]->firstname)) echo ', '.$rows[0]->firstname.'</b>'; };
if($params->display == 1) { if (isset($rows[0]->firstname)) echo $rows[0]->firstname.' '; if (isset($rows[0]->lastname)) echo $rows[0]->lastname; };
if($params->link_name == 1) echo '</a>';?>

</div>
